package algorithm.leetcode.array;

/**
 * @auther zhouliangliang
 * @date 2022/7/12 13:33
 * @description 螺旋输出数组
 */
public class SpiralOutArray {

	private int[][] array;

	private int rightBoundary;
	private int bottomBoundary;
	private int leftBoundary;
	private int topBoundary;
	private int lineIndex = 0;
	private int columnIndex = 0;

	public SpiralOutArray(int[][]array) {
		this.array = array;
		this.rightBoundary = array[0].length;
		this.bottomBoundary = array.length;
		this.leftBoundary = 0;
		this.topBoundary = 0;
	}

	private void moveRight(int[][]array, int i, int j, int rightBoundary) {
		while (j < rightBoundary) {
			System.out.println(array[i][j]);
			j = j+1;
		}
		lineIndex = i+1;
		columnIndex = j-1;
		topBoundary = topBoundary + 1;
	}

	private void moveDown(int[][]array, int i, int j, int bottomBoundary) {
		while (i < bottomBoundary) {
			System.out.println(array[i][j]);
			i = i + 1;
		}
		lineIndex = i-1;
		columnIndex = j-1;
		rightBoundary = rightBoundary - 1;
	}

	private void moveLeft(int[][]array, int i, int j, int leftBoundary) {
		while (j >= leftBoundary) {
			System.out.println(array[i][j]);
			j = j-1;
		}
		lineIndex = i - 1;
		columnIndex = j + 1;
		bottomBoundary = bottomBoundary - 1;
	}

	private void moveTop(int[][] array, int i, int j, int topBoundary) {
		while (i >= topBoundary) {
			System.out.println(array[i][j]);
			i = i - 1;
		}
		lineIndex = i + 1;
		columnIndex = j + 1;
		leftBoundary = leftBoundary + 1;
	}

	private String nextAction(String initAction) {
		if("moveRight".equals(initAction)) {
			return "moveDown";
		} else if("moveDown".equals(initAction)) {
			return "moveLeft";
		} else if("moveLeft".equals(initAction)) {
			return "moveTop";
		} else {
			return "moveRight";
		}
	}

	private void control() {
		String action = "moveRight";
		while( !(leftBoundary == rightBoundary || topBoundary == bottomBoundary)) {
			switch (action) {
				case "moveRight":
					moveRight(array, lineIndex, columnIndex, rightBoundary);
					action = nextAction("moveRight");
					break;
				case "moveDown":
					moveDown(array, lineIndex, columnIndex, bottomBoundary);
					action = nextAction("moveDown");
					break;
				case "moveLeft":
					moveLeft(array, lineIndex, columnIndex, leftBoundary);
					action = nextAction("moveLeft");
					break;
				case "moveTop":
					moveTop(array, lineIndex, columnIndex, topBoundary);
					action = nextAction("moveTop");
					break;
			}
		}
	}

	public static void main(String[] args) {
		int[][]array1 = new int[][]{
			{1,  2,  3,  4,  5, 6, 7, 8, 9, 10},
			{24, 25, 26, 27, 28,29,30,31,32,11},
			{41, 40, 39, 38, 37,36,35,34,33,12},
			{22, 21, 20, 19, 18,17,16,15,14,13},
		};


		int[][]array2 = new int[][]{
			{1,  2,  3,  4},
			{24,  25, 26,  5},
			{23,  40,  27,  6},
			{22,  39,  28,  7},
			{21,  38,  29,  8},
			{20,  37,  30,  9},
			{19,  36,  31,  10},
			{18,  35,  32,  11},
			{17,  34,  33,  12},
			{16,  15,  14,  13},
		};

		SpiralOutArray t = new SpiralOutArray(array2);
		t.control();
	}
}
